<?php

/**
 * IndexController.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysmanger\Controller;

use System\Common\SystemController;

class IndexController extends SystemController {
    public $limit = 20;

    public function index()
    {
        $this->system_title = '账号列表';
        $page = rintval(I('get.p', 1));
        $start = null;
        $limit = $this->limit;
        list($start, $limit, $page) = page_limit($page, $limit);
        $params = array(
                'fields'=>'mid,user,passwd,salt,role_id,created',
                'start'=> $start,
                'limit'=>$limit,
                'order'=>'mid asc',
        );
        $obj_serv = D('Manger','Service');
        $result = $obj_serv->getMangerList($params);
        $count = 0;
        $list = array();
        if($result)
        {
            $this->_assignPage($result['count'], $limit);
            $count = $result['count'];
            $list = $result['list'];
        }
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->loadSystemView();
    }
    
    public function add()
    {
        $this->system_title = '添加子账号';
        $obj_serv = D('Manger','Service');
        $role_list = $obj_serv->getRole(null, 'role_id,role_name');
        $act_url = U('Index/dosubmit');
        $this->assign('role_list', $role_list);
        $this->assign('act_url', $act_url);
        $this->loadSystemView('edit');
    }
    
    public function edit()
    {
        $this->system_title = '编辑子账号';
        $mid = rintval(I('get.mid'));
        if(!$mid)
        {
            return $this->error('非法操作');
        }
        
        $obj_serv = D('Manger','Service');
        $data = $obj_serv->getManger($mid, 'user,role_id');
        if(!$data)
        {
            return $this->error('此子账号不存在');
        }
        
        $role_list = $obj_serv->getRole(null, 'role_id,role_name');
        $act_url = U('Index/dosubmit');
        $data['mid'] = $mid;
        $this->assign('role_list', $role_list);
        $this->assign('act_url', $act_url);
        $this->assign('data', $data);
        $this->loadSystemView();
    }
    
    
    public function dosubmit()
    {
        $request = I('post.');
        $obj_serv = D('Manger','Service');
        try {
            $obj_serv->saveManger($request);
        } catch (\Exception $e) {
            
            $msg = $e->getMessage();
            
            return $this->error($msg);
        }
        
        return $this->success('保存成功', U('Index/index'));
    }
    
    // 删除账号
    public function del()
    {
        try {
            $mid = I('get.mid');
            $obj_serv = D('Manger','Service');
            $rs = $obj_serv->delManger(rintval($mid));
            if(!$rs)
            {
                throw new \Exception('删除失败');
            }
        } catch (\Exception $e) {
            
            return $this->error($e->getMessage());
        }
        
        return $this->success('删除成功',U('Index/index'));
    }

}
 
 